package com.top.horizon.th1804app.ui.activity;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.top.horizon.th1804app.R;
import com.top.horizon.th1804app.base.BaseActivity;
import com.top.horizon.th1804app.base.BaseFragment;
import com.top.horizon.th1804app.ui.fragment.DeviceDetailFragment;
import com.top.horizon.th1804app.ui.fragment.FileManagementFragment;
import com.top.horizon.th1804app.ui.fragment.PlayBackFragment;
import com.top.horizon.th1804app.ui.fragment.SearchFragment;
import com.top.horizon.th1804app.ui.fragment.SettingFragment;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private static Fragment mContainer;
    private FragmentManager fm;

    private TextView tvTitle;
    private ImageView ivBack;
    private RadioGroup rgDeviceCustodian;
    private DeviceDetailFragment deviceDetailFragment;
    private FileManagementFragment fileManagementFragment;
    private SettingFragment settingFragment;
    private SearchFragment searchFragment;
    private PlayBackFragment playBackFragment;
    private boolean isPlayBack = false;
    //用于文件管理界面展示判断展示哪个fragment
    private final int SHOW_SEARCH = 0;
    private final int SHOW_RESUTL = 1;
    private final int SHOW_PLAY = 2;
    private int mFileShowStatus = SHOW_SEARCH;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        tvTitle = findViewById(R.id.tv_title);
        ivBack = findViewById(R.id.iv_back);
        rgDeviceCustodian = findViewById(R.id.rg_device_custodian);

        rgDeviceCustodian.setOnCheckedChangeListener(this);

        //点击左上角返回
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果是回放页就切到搜索列表页
                if (isPlayBack) {
                    if (searchFragment == null) {
                        searchFragment = new SearchFragment();
                    }
                    //改变显示状态
                    mFileShowStatus = SHOW_RESUTL;
                    switchContent(searchFragment);
                    isPlayBack = false;
                    ivBack.setVisibility(View.VISIBLE);
                    tvTitle.setText("搜索结果");
                } else {
                    //否则就到文件管理页
                    if (fileManagementFragment == null) {
                        fileManagementFragment = new FileManagementFragment();
                    }
                    //改变显示状态
                    mFileShowStatus = SHOW_SEARCH;
                    tvTitle.setText("文件管理");
                    ivBack.setVisibility(View.GONE);
                    switchContent(fileManagementFragment);
                }

            }
        });
    }

    @Override
    protected void initData() {
        //首次进入加载首页的fragment
        fm = getSupportFragmentManager();
        if (deviceDetailFragment == null) {
            deviceDetailFragment = new DeviceDetailFragment();
        }
        tvTitle.setText("TH1804设备管理");
        switchContent(deviceDetailFragment);

        //在文件管理页点击搜索按钮，跳转到查询结果页
        if (fileManagementFragment == null) {
            fileManagementFragment = new FileManagementFragment();
        }
        fileManagementFragment.setOnClickSearchListener(new FileManagementFragment.OnClickSearchListener() {
            @Override
            public void onClickSearch(View view) {
                if (searchFragment == null) {
                    searchFragment = new SearchFragment();
                }
                //改变显示状态
                mFileShowStatus = SHOW_RESUTL;
                switchContent(searchFragment);
                isPlayBack = false;
                ivBack.setVisibility(View.VISIBLE);
                tvTitle.setText("搜索结果");
            }
        });
        fileManagementFragment.setOnClickPlayBackListener(new SearchFragment.OnClickPlayBackListener() {
            @Override
            public void onClickPlayBack() {
                if (playBackFragment == null) {
                    playBackFragment = new PlayBackFragment();
                }
                //改变显示状态
                mFileShowStatus = SHOW_PLAY;
                switchContent(playBackFragment);
                isPlayBack = true;
                ivBack.setVisibility(View.VISIBLE);
                tvTitle.setText("播放");
            }
        });

        //在查询结果表点击回放按钮，跳转到回放页
        if (searchFragment == null) {
            searchFragment = new SearchFragment();
        }
        searchFragment.setOnClickPlayBackListener(new SearchFragment.OnClickPlayBackListener() {
            @Override
            public void onClickPlayBack() {
                if (playBackFragment == null) {
                    playBackFragment = new PlayBackFragment();
                }
                //改变显示状态
                mFileShowStatus = SHOW_PLAY;
                switchContent(playBackFragment);
                isPlayBack = true;
                ivBack.setVisibility(View.VISIBLE);
                tvTitle.setText("播放");
            }
        });
    }

    /**
     * 底部选择按钮点击事件
     *
     * @param group
     * @param checkedId
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        ivBack.setVisibility(View.GONE);
        switch (checkedId) {
            case R.id.rb_device_detail:
                //切到设备信息页
                if (deviceDetailFragment == null) {
                    deviceDetailFragment = new DeviceDetailFragment();
                }
                tvTitle.setText("TH1804设备管理");

                switchContent(deviceDetailFragment);
                break;
            case R.id.rb_file_management:
                BaseFragment fragment = null;
                if (mFileShowStatus == SHOW_SEARCH) {
                    //切到文件管理页
                    if (fileManagementFragment == null) {
                        fileManagementFragment = new FileManagementFragment();
                    }
                    tvTitle.setText("文件管理");
                    fragment = fileManagementFragment;
                } else if (mFileShowStatus == SHOW_RESUTL) {
                    ivBack.setVisibility(View.VISIBLE);
                    if (searchFragment == null) {
                        searchFragment = new SearchFragment();
                    }
                    tvTitle.setText("搜索结果");
                    fragment = searchFragment;
                } else {
                    ivBack.setVisibility(View.VISIBLE);
                    if (playBackFragment == null) {
                        playBackFragment = new PlayBackFragment();
                    }
                    tvTitle.setText("搜索结果");
                    fragment = playBackFragment;
                }
                switchContent(fragment);
                break;
            case R.id.rb_setting:
                //切到设置页
                if (settingFragment == null) {
                    settingFragment = new SettingFragment();
                }
                tvTitle.setText("设置");
                switchContent(settingFragment);
                break;
        }
    }

    public void switchContent(Fragment to) {
        if (mContainer != to) {
            FragmentTransaction transaction = fm.beginTransaction();
            transaction.replace(R.id.container, to).commit();
            mContainer = to;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

    }
}
